<?php

namespace app\controllers;

use Yii;
use app\tools\Re;
use app\tools\JPushHelper;
use app\models\FormType;
use app\models\Repair;
use app\models\Article;

class ApiController extends BaseController
{
  public function actionTestPush(){
    $user_arr = [
      'id' => '12',
    ];
    $extra = [
      'push_type' => 'qxdd',
      'push_user_type' => 3,
    ];
    $result = JPushHelper::pushNotifyByReg($user_arr, '测试推送标题', '测试推送内容', $extra);
    var_dump($result);
  }

  //wap端相关模块详情详情
  public function actionInfoWap(){
    $type = Yii::$app->request->post('type', 1); //1:施工单位  2:资讯   3:表单
    $id = Yii::$app->request->post('id', 0);
    if ($type == 1) {
      $repair = Repair::findOne($id);
      if (!$repair) {
        return Re::data(null, '施工单位信息读取失败', Re::ERROR);
      }
      $data = [
        'id' => $repair->id,
        'content' => $repair->desc,
        //'content' => '',
      ];
      return Re::data(['data' => $data]);
    } else if ($type == 2) {
      $article = Article::findOne($id);
      if (!$article) {
        return Re::data(null, '资讯信息读取失败', Re::ERROR);
      }
      $data = [
        'id' => $article->id,
        'title' => $article->title,
        'content' => $article->content,
        //'content' => '',
        'source' => $article->source,
        'created_time' => date("Y-m-d", $article->created_at),
      ];
      return Re::data(['data' => $data]);
    } else if ($type == 3) {
      $form_type = FormType::findOne($id);
      if (!$form_type) {
        return Re::data(null, '表单信息读取失败', Re::ERROR);
      }
      $data = [
        'id' => $form_type->id,
        'content' => $form_type->desc,
        //'content' => '',
      ];
      return Re::data(['data' => $data]);
    } else {
      return Re::data(null, '参数异常', Re::ERROR);
    }
  }

  public function actionInfo(){
    $type = Yii::$app->request->post('type', 1); //1:施工单位  2:资讯   3:表单
    $id = Yii::$app->request->post('id', 0);
    $android = (int)Yii::$app->request->post('android', 0);
    if ($type == 1) {
      $repair = Repair::findOne($id);
      if (!$repair) {
        return Re::data(null, '施工单位信息读取失败', Re::ERROR);
      }
      $data = [
        'id' => $repair->id,
        //'content' => $android ? $repair->desc : $this->getContent($repair->desc),
        'content' => $repair->desc,
      ];
      return Re::data(['data' => $data]);
    } else if ($type == 2) {
      $article = Article::findOne($id);
      if (!$article) {
        return Re::data(null, '资讯信息读取失败', Re::ERROR);
      }
      $data = [
        'id' => $article->id,
        'title' => $article->title,
        //'content' => $android ? $article->content : $this->getContent($article->content),
        'content' => $article->content,
        'source' => $article->source,
        'created_time' => date("Y-m-d", $article->created_at),
      ];
      return Re::data(['data' => $data]);
    } else if ($type == 3) {
      $form_type = FormType::findOne($id);
      if (!$form_type) {
        return Re::data(null, '表单信息读取失败', Re::ERROR);
      }
      $data = [
        'id' => $form_type->id,
        //'content' => $android ? $form_type->desc : $this->getContent($form_type->desc),
        'content' => $form_type->desc,
      ];
      return Re::data(['data' => $data]);
    } else {
      return Re::data(null, '参数异常', Re::ERROR);
    }
  }

  private function getContent($content) {
    $content= str_replace("&nbsp;", "",$content);
    $content = strip_tags($content);
    $content = trim($content);
    return $content;
  }
}
